using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using AutoMapper;
using SapaFinance.DTO;
using SapaFinance.Domain.Model;
using SapaFinance.OOMap.Helpers;
using SapaFinance.Domain.Extensions;
using Telerik.Web.Mvc.UI;

namespace SapaFinance.OOMap
{
    public class DomainToViewModelMappingProfile : Profile
    {
        public override string ProfileName
        {
            get { return "DomainToViewModelMapping"; }
        }

        protected override void Configure()
        {
            //user
            Mapper.CreateMap<User, UserDTO>();
            Mapper.CreateMap<User, SelectListItem>()
           .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
           .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.FullName));
            //currency
            Mapper.CreateMap<Currency, CurrencyDTO>()
                .ForMember(dest => dest.TypeName, opt => opt.MapFrom(src => src.GetType().BaseType.Name));
            //request
            Mapper.CreateMap<Request, RequestDTO>().ConvertUsing<RequestToRequestDTOConvert>();
            //cfo
            Mapper.CreateMap<CFO, CfoDTO>();
            Mapper.CreateMap<CFO, IEnumerable<SelectListItem>>().ConvertUsing<CFOToSelectListConvert>();
            Mapper.CreateMap<CFO, TreeViewItem>()
                .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID));

            Mapper.CreateMap<CFO, SelectListItem>()
             .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
             .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Name));
            //budgetitem
            Mapper.CreateMap<BudgetItem, SelectListItem>()
                .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
                .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Name));
            Mapper.CreateMap<BudgetItem, IEnumerable<SelectListItem>>().ConvertUsing<BudgetItemToSelectListConvert>();
            Mapper.CreateMap<BudgetItem, BudgetItemDTO>()
                .ForMember(dest => dest.UID, opt => opt.MapFrom(src => src.UID))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name));
            //agent
            Mapper.CreateMap<Agent, AgentDTO>()
                .ForMember(dest => dest.UID, opt => opt.MapFrom(src => src.UID))
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
                .ForMember(dest => dest.IsSelfAgent, opt => opt.MapFrom(src => src.IsSelfAgent));
            Mapper.CreateMap<Agent, SelectListItem>()
                .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
                .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Name));
            //bank
            Mapper.CreateMap<Bank, BankDTO>().ConvertUsing<BankToBankDTOConvert>();
            Mapper.CreateMap<Bank, SelectListItem>()
                .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
                .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Name));
            Mapper.CreateMap<Bank, IEnumerable<SelectListItem>>().ConvertUsing<BankToSelectListConvert>();
            //currency
            Mapper.CreateMap<Currency, SelectListItem>()
                .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
                .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Name));
            Mapper.CreateMap<Currency, IEnumerable<SelectListItem>>().ConvertUsing<CurrencyToSelectListConvert>();
            //paymenttype
            Mapper.CreateMap<PaymentType, SelectListItem>()
                .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
                .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Name));
            Mapper.CreateMap<PaymentType, IEnumerable<SelectListItem>>().ConvertUsing<PaymentTypeToSelectListConvert>();
            //corporation
            Mapper.CreateMap<Corporation, IEnumerable<SelectListItem>>().ConvertUsing<CorporationToSelectListConvert>();
            Mapper.CreateMap<Corporation, SelectListItem>()
                .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
                .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Name));
            Mapper.CreateMap<Corporation, CorporationDTO>().ConvertUsing<CorporationToCorporationDTOConvert>();
            //account
            Mapper.CreateMap<Account, AccountDTO>().ConvertUsing<AccountToAccountDTOConvert>();
            Mapper.CreateMap<Account, SelectListItem>()
                 .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.UID.ToString()))
                .ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Number));
            Mapper.CreateMap<Account, IEnumerable<SelectListItem>>().ConvertUsing<AccountToSelectListConvert>();
            Mapper.CreateMap<Account, CurrencyDetailDTO>()
                .ForMember(dest => dest.BankName, opt => opt.MapFrom(src => src.Bank.Name))
                .ForMember(dest => dest.CurrencyName, opt => opt.MapFrom(src => src.Currency.Name));
            //order
            Mapper.CreateMap<IEnumerable<Request>, OrderDTO>().ConvertUsing<RequestToOrderDTOConvert>();

            Mapper.CreateMap<Order, OrderDTO>().ConvertUsing<OrderToOrderDTOConvert>();
        }
    }

    
}